<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 股票管理
 */

namespace Admin\Controller;
use Model\StockCompanyModel;
use Model\StockHandicapModel;
use Model\StockModel;
use Model\StockNoticeModel;
use Model\TradePlaceModel;
use Model\TradeSetModel;

class StockController extends BaseController{

    //交易设置
    public function set(){
        return;
        $tsModel = new TradeSetModel();
        if(IS_POST){
            $data = $_POST;
            $data['week_list'] = implode(',',$_POST['week_list']);
            $tsRes = $tsModel->findData(['trade_key'=>'rang_space']);
            if($tsRes){
                foreach ($data as $key=>$val){
                    $tsModel->editData(['trade_key'=>$key],['trade_val'=>$val]);
                }
            }else{
                foreach ($data as $key=>$val){
                    $tsModel->addData(['trade_key'=>$key,'trade_val'=>$val]);
                }
            }
            $this->logModel->log('交易设置','trade_set',0);
            $this->printOut(['code'=>0]);
        }
        $tsList = $tsModel->selectData([]);
        $tsRes = [];
        foreach ($tsList as $val){
            $tsRes[$val['trade_key']] = $val['trade_val'];
        }
        $this->assign('tsRes',$tsRes);
        $this->display('stock/set');
    }

    //交易所列表
    public function place(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['name'] = array('like','%'.$sessionArr['name'].'%');
        }
        $tpModel = new TradePlaceModel();
        $resList = $tpModel->search($where,$page,$this->pernum);
        $totalNum = $tpModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->display('stock/place');
    }
    //添加交易所
    public function addPlace(){
        if(IS_POST){
            unset($_POST['__hash__']);
            $data = $_POST;
            $data['week_list'] = implode(',',$_POST['week_list']);
            $data['addtime'] = $this->dateNow;
            $tpModel = new TradePlaceModel();
            $id = $tpModel->addData($data);
            $this->logModel->log('添加交易所','trade_place',$id);
            redirect($this->domianUrl.'/stock/place?tip=1');
        }
        //获取分组
        $this->display('stock/addplace');
    }
    //编辑交易所
    public function editPlace(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            unset($_POST['__hash__']);
            unset($_POST['id']);
            $data = $_POST;
            $data['week_list'] = implode(',',$_POST['week_list']);
            $tpModel = new TradePlaceModel();
            $tpModel->editData($where,$data);
            $this->logModel->log('编辑交易所','trade_place',$where['id']);
            redirect($this->domianUrl.'/stock/place?p='.$_POST['page'].'&tip=1');
        }
        //获取组内容
        $this->assign('page',$_GET['page']);
        $tpModel = new TradePlaceModel();
        $selfInfo = $tpModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        $this->display('stock/editplace');
    }
    //冻结解冻交易所
    public function freezePlace(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $tpModel = new TradePlaceModel();
        $tpModel->editData($where,$data);
        $this->logModel->log('冻结|解冻交易所','trade_place',$_GET['id']);
        redirect($this->domianUrl.'/stock/place?p='.$_GET['page'].'&tip=1');
    }
    //删除交易所
    public function delPlace(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $tpModel = new TradePlaceModel();
        $tpModel->editData($where,$data);
        $this->logModel->log('删除交易所','trade_place',$_GET['id']);
        redirect($this->domianUrl.'/stock/place?p='.$_GET['page'].'&tip=1');
    }


    //股票列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['tpid']  = @$_POST['tpid'] ? $_POST['tpid'] : @$sessionParam['tpid'];
        $sessionArr['stockno']  = @$_POST['stockno'] ? $_POST['stockno'] : @$sessionParam['stockno'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['tpid']){
            $where['tpid'] = $sessionArr['tpid'];
        }
        if($sessionArr['stockno']){
            $where['stockno'] = $sessionArr['stockno'];
        }
        if($sessionArr['name']){
            $where['stock_name'] = array('like','%'.$sessionArr['name'].'%');
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $sModel = new StockModel();
        $resList = $sModel->search($where,$page,$this->pernum);
        $totalNum = $sModel->getTotalNum($where);
        $sidArr = [];
        $tpidArr = [];
        foreach ($resList as $val){
            $sidArr[] = $val['id'];
            $tpidArr[] = $val['tpid'];
        }
        $tpRes = [];
        $shRes = [];
        if($sidArr){
            $tpModel = new TradePlaceModel();
            $tpList = $tpModel->selectData(['id'=>['in',$tpidArr]]);
            foreach ($tpList as $val){
                $tpRes[$val['id']] = $val['name'];
            }

            $shModel = new StockHandicapModel();
            $shList = $shModel->selectData(['sid'=>['in',$sidArr]],'sid,stock_num');
            foreach ($shList as $val){
                $shRes[$val['sid']] = $val['stock_num'];
            }
        }
        $pageShow = getPage($totalNum,$this->pernum);
        $tpModel = new TradePlaceModel();
        $allPlace = $tpModel->allPlace();
        $this->assign('allPlace',$allPlace);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('shRes',$shRes);
        $this->assign('tpRes',$tpRes);
        $this->display('stock/index');
    }
    //添加股票
    public function addData(){
        if(IS_POST){
            $sModel = new StockModel();
            $scModel = new StockCompanyModel();
            $shModel = new StockHandicapModel();
            $sRes = $sModel->findData(['stock_no'=>$_POST['stock_no'],'status'=>['neq',3]]);
            if($sRes){
                redirect($this->domianUrl.'/stock/addData?tip=2&errmsg='.urlencode(urlencode('股票代码不能重复')));
            }
            $sData = [
                'tpid'=>$_POST['tpid'],
                'stock_no'=>$_POST['stock_no'],
                'stock_name'=>$_POST['stock_name'],
                'begin_price'=>$_POST['begin_price'],
                'addtime'=>$this->dateNow,
                'status'=>2
            ];
            $sid = $sModel->addData($sData);

            foreach ($_POST['cpname'] as $key=>$val){
                $scData = [
                    'sid'=>$sid,
                    'name'=>$val,
                    'percent'=>$_POST['stocknum'][$key]
                ];
                $scModel->addData($scData);
            }
            $shData = [
                'sid'=>$sid,
                'stock_num'=>$_POST['stock_num'],
                'tpid'=>$_POST['tpid']
            ];
            $shModel->addData($shData);

            $this->logModel->log('添加股票','stock',$sid);
            redirect($this->domianUrl.'/stock/index?tip=1');
        }

        $tpModel = new TradePlaceModel();
        $allPlace = $tpModel->allPlace();
        $this->assign('allPlace',$allPlace);
        $this->display('stock/adddata');
    }
    //编辑股票
    public function editData(){
        if(IS_POST){
            $sModel = new StockModel();
            $scModel = new StockCompanyModel();
            $shModel = new StockHandicapModel();
            $sRes = $sModel->findData(['stock_no'=>$_POST['stock_no'],'status'=>['neq',3]]);
            if($sRes && $sRes['id']!=$_POST['id']){
                redirect($this->domianUrl.'/stock/editData?id='.$_POST['id'].'&page='.$_POST['page'].'&tip=2&errmsg='.urlencode(urlencode('股票代码不能重复')));
            }

            $sData = [
                'tpid'=>$_POST['tpid'],
                'stock_no'=>$_POST['stock_no'],
                'stock_name'=>$_POST['stock_name'],
                'begin_price'=>$_POST['begin_price'],
                'addtime'=>$this->dateNow,
                'status'=>2
            ];
            $sid = $_POST['id'];
            $sModel->editData(['id'=>$sid],$sData);

            $scModel->delData(['sid'=>$sid]);
            foreach ($_POST['cpname'] as $key=>$val){
                $scData = [
                    'sid'=>$sid,
                    'name'=>$val,
                    'percent'=>$_POST['stocknum'][$key]
                ];
                $scModel->addData($scData);
            }
            $shData = [
                'stock_num'=>$_POST['stock_num'],
                'tpid'=>$_POST['tpid']
            ];
            $shModel->editData(['sid'=>$sid],$shData);

            $this->logModel->log('编辑股票','stock',$sid);
            redirect($this->domianUrl.'/stock/index?p='.$_POST['page'].'&tip=1');
        }
        //获取组内容
        $this->assign('page',$_GET['page']);
        $sModel = new StockModel();
        $selfInfo = $sModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        $tpModel = new TradePlaceModel();
        $allPlace = $tpModel->allPlace();
        $this->assign('allPlace',$allPlace);
        $scModel = new StockCompanyModel();
        $scList = $scModel->selectData(['sid'=>$selfInfo['id']]);
        $shModel = new StockHandicapModel();
        $shRes = $shModel->findData(['sid'=>$selfInfo['id']]);
        $this->assign('scList',$scList);
        $this->assign('shRes',$shRes);
        $this->display('stock/editdata');
    }
    //删除股票
    public function delData(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $sModel = new StockModel();
        $sModel->editData($where,$data);
        $shModel = new StockHandicapModel();
        $shModel->editData(['sid'=>$_GET['id']],$data);
        $this->logModel->log('删除股票','stock',$_GET['id']);
        redirect($this->domianUrl.'/stock/index?p='.$_GET['page'].'&tip=1');
    }
    //冻结解冻股票
    public function freezeData(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $sModel = new StockModel();
        $sModel->editData($where,$data);
        $shModel = new StockHandicapModel();
        $shModel->editData(['sid'=>$_GET['id']],$data);
        $this->logModel->log('冻结|解冻股票','stock',$_GET['id']);
        redirect($this->domianUrl.'/stock/index?p='.$_GET['page'].'&tip=1');
    }

    //公告列表
    public function notice(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['stockno']  = @$_POST['stockno'] ? $_POST['stockno'] : @$sessionParam['stockno'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['stockno']){
            $sModel = new StockModel();
            $sRes = $sModel->findData(['stock_no'=>$sessionArr['stockno']]);
            $where['sid'] = $sRes['id'] ? $sRes['id'] : 0;
        }
        if($sessionArr['name']){
            $where['title'] = array('like','%'.$sessionArr['name'].'%');
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $snModel = new StockNoticeModel();
        $resList = $snModel->search($where,$page,$this->pernum);
        $totalNum = $snModel->getTotalNum($where);
        $sidArr = [];
        foreach ($resList as $val){
            $sidArr[] = $val['sid'];
        }
        $sRes = [];
        if($sidArr){
            $sModel = new StockModel();
            $sList = $sModel->selectData(['id'=>['in',$sidArr]]);
            foreach ($sList as $val){
                $sRes[$val['id']] = $val['stock_name'];
            }
        }
        $pageShow = getPage($totalNum,$this->pernum);
        $tpModel = new TradePlaceModel();
        $allPlace = $tpModel->allPlace();
        $this->assign('allPlace',$allPlace);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('sRes',$sRes);
        $this->display('stock/notice');
    }
    //添加公告
    public function addNotice(){
        if(IS_POST){
            $snModel = new StockNoticeModel();
            $sData = [
                'sid'=>$_POST['twotype'],
                'title'=>$_POST['title'],
                'content'=>$_POST['content'],
                'addtime'=>$this->dateNow,
            ];
            $snid = $snModel->addData($sData);
            $this->logModel->log('添加股票公告','stock_notice',$snid);
            $this->printOut(['code'=>0]);
        }
        $tpModel = new TradePlaceModel();
        $allPlace = $tpModel->allPlace();
        $this->assign('allPlace',$allPlace);
        $this->display('stock/addnotice');
    }
    //编辑公告
    public function editNotice(){
        if(IS_POST){
            $id = $_POST['id'];
            $snModel = new StockNoticeModel();
            $sData = [
                'sid'=>$_POST['twotype'],
                'title'=>$_POST['title'],
                'content'=>$_POST['content']
            ];
            $snid = $snModel->editData(['id'=>$id],$sData);
            $this->logModel->log('编辑股票公告','stock_notice',$snid);
            $this->printOut(['code'=>0]);
        }
        //获取组内容
        $this->assign('page',$_GET['page']);
        $snModel = new StockNoticeModel();
        $selfInfo = $snModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        //获取交易所id
        $sModel = new StockModel();
        $sRes = $sModel->findData(['id'=>$selfInfo['sid']]);
        $this->assign('tpid',$sRes['tpid']);
        $tpModel = new TradePlaceModel();
        $allPlace = $tpModel->allPlace();
        $this->assign('allPlace',$allPlace);
        $this->display('stock/editnotice');
    }
    //删除公告
    public function delNotice(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $snModel = new StockNoticeModel();
        $snModel->editData($where,$data);
        $this->logModel->log('删除股票公告','stock',$_GET['id']);
        redirect($this->domianUrl.'/stock/notice?p='.$_GET['page'].'&tip=1');
    }
    //冻结解冻公告
    public function freezeNotice(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $snModel = new StockNoticeModel();
        $snModel->editData($where,$data);
        $this->logModel->log('冻结|解冻股票公告','stock',$_GET['id']);
        redirect($this->domianUrl.'/stock/notice?p='.$_GET['page'].'&tip=1');
    }
}